package com.meng.mmlive.ui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.lqr.adapter.LQRAdapterForRecyclerView;
import com.lqr.adapter.LQRViewHolderForRecyclerView;
import com.meng.mmlive.R;

import java.util.List;

import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.ReadReceiptInfo;
import io.rong.message.TextMessage;

/**
 * Created by Administrator on 2017/5/15.
 */

public class SessionRecyclerViewAdapter extends LQRAdapterForRecyclerView<Message> {
    private static final int SEND_TEXT = R.layout.item_session_text_right;
    private static final int RECEIVE_TEXT = R.layout.item_session_text_left;

    private Context mContext;
    private List<Message> mData;
    public SessionRecyclerViewAdapter(Context context, List<Message> data) {
        super(context, data);
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public void convert(LQRViewHolderForRecyclerView helper, Message item, int position) {
        MessageContent msgContent = item.getContent();
        if (msgContent instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) msgContent;
            helper.setText(R.id.msg_tv,textMessage.getContent()+"\n"+item.getSentTime());

            ReadReceiptInfo readReceiptInfo = item.getReadReceiptInfo();
            Log.d("SessionRecyclerViewAdap", position+" readReceiptInfo:" + readReceiptInfo);
            if(readReceiptInfo  != null){
                Log.d("SessionRecyclerViewAdap", position+" receivedStatus.isRead():" + readReceiptInfo.isReadReceiptMessage());
            }
        }
        Log.d("SessionRecyclerViewAdap",position+ " item.getSentStatus():" + item.getSentStatus());
        if(item.getMessageDirection() == Message.MessageDirection.SEND){
            switch (item.getSentStatus()){
                case READ:
                    helper.setText(R.id.msg_status_tv,"已读");
                    helper.setBackgrounResource(R.id.msg_status_tv,R.drawable.shape_msg_readedstatus_bg);
                    helper.getView(R.id.msg_status_tv).setVisibility(View.VISIBLE);
                    break;
                case SENT:
                    if(item.getSentTime() <= ntfTime){
                        helper.setText(R.id.msg_status_tv,"已读");
                        helper.setBackgrounResource(R.id.msg_status_tv,R.drawable.shape_msg_readedstatus_bg);
                    }else{
                        helper.setText(R.id.msg_status_tv,"送达");
                        helper.setBackgrounResource(R.id.msg_status_tv,R.drawable.shape_msg_arrivestatus_bg);
                    }
                    helper.getView(R.id.msg_status_tv).setVisibility(View.VISIBLE);
                    break;
                case FAILED:
                    helper.setText(R.id.msg_status_tv,"失败");
                    helper.setBackgrounResource(R.id.msg_status_tv,R.drawable.shape_msg_failedstatus_bg);
                    helper.getView(R.id.msg_status_tv).setVisibility(View.VISIBLE);
                    break;
                case SENDING:
                    helper.getView(R.id.msg_status_tv).setVisibility(View.GONE);
                    break;
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        Message msg = mData.get(position);
        boolean isSend = msg.getMessageDirection() == Message.MessageDirection.SEND ? true : false;
        MessageContent msgContent = msg.getContent();
        if (msgContent instanceof TextMessage) {
            return isSend ? SEND_TEXT : RECEIVE_TEXT;
        }
        return -1;
    }
    private long ntfTime;
    public void updateMsgReadedStatus(long ntfTime) {
        this.ntfTime = ntfTime;
        notifyDataSetChanged();
    }

    public void updateMessageStatus(int messageId, Message.SentStatus sentStatus) {
        for(int i = 0;i < mData.size();i++){
            if(mData.get(i).getMessageId() == messageId){
                mData.get(i).setSentStatus(sentStatus);
                notifyItemChanged(i);
                break;
            }
        }
    }
}
